package com.heima.article.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApCollectionService;
import com.heima.article.service.ArticleContentService;
import com.heima.article.service.ArticleScoreService;
import com.heima.model.article.dtos.*;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/v1/article")
public class ArticleHomeController {

    @Autowired
    private ApCollectionService apCollectionService;
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ArticleContentService contentService;
    @Autowired
    private ArticleScoreService articleScoreService;

    @PostMapping("/detail")
    public ResponseResult getArticleDetail(@RequestBody ArticleInfoDto dto){
        return contentService.getContent(dto.getArticleId());
    }
    @PostMapping("/save")
    public ResponseResult saveArticleDetail(@RequestBody SaveArticleDto dto){
        ArticleDto articleDto = new ArticleDto();
        articleDto.setContent(dto.getContent());
        articleDto.setAuthorId(dto.getAuthorId());
        articleDto.setTitle(dto.getTitle());
        articleDto.setAuthorName(dto.getAuthorName());
        articleDto.setChannelName(dto.getChannel());
        articleDto.setCreatedTime(new Date());
        articleDto.setPublishTime(new Date());
        articleDto.setFlag((byte) 0);
        articleDto.setCollection(0);
        return apArticleService.saveArticle(articleDto);
    }
    @PostMapping("/all")
    public ResponseResult getAll(@RequestBody UserDto dto){
        return apArticleService.getAll(dto);
    }
    @PostMapping("/history")
    public ResponseResult getHistory(@RequestBody UserDto dto){
        return apArticleService.getHistory(dto);
    }

    @PostMapping("/collect")
    public ResponseResult collect(@RequestBody CollectionBehaviorDto dto) {
        return apCollectionService.collection(dto);
    }

    @PostMapping("/grade")
    public ResponseResult grade(@RequestBody ScoreDto dto) {
        return articleScoreService.Score(dto);
    }

    @GetMapping ("/hot")
    public ResponseResult getHotList() {
        QueryWrapper<ApArticle> apArticleQueryWrapper = new QueryWrapper<>();
        apArticleQueryWrapper.orderByDesc("collection");
        apArticleQueryWrapper.last("LIMIT 10");
        List<ApArticle> list = apArticleService.list(apArticleQueryWrapper);
        return ResponseResult.okResult(list);
    }
    @GetMapping("/getMore")
    public ResponseResult getMore() {
        QueryWrapper<ApArticle> apArticleQueryWrapper = new QueryWrapper<>();
        apArticleQueryWrapper.last("order by rand() LIMIT 6");
        List<ApArticle> list = apArticleService.list(apArticleQueryWrapper);

        return ResponseResult.okResult(list);
    }
    @GetMapping("/getTop")
    public ResponseResult getTop() {
        QueryWrapper<ApArticle> apArticleQueryWrapper = new QueryWrapper<>();
        apArticleQueryWrapper.eq("flag",1);
        apArticleQueryWrapper.orderByDesc("created_time");
        apArticleQueryWrapper.last("limit 3");
        List<ApArticle> list = apArticleService.list(apArticleQueryWrapper);
        return ResponseResult.okResult(list);
    }
    @PostMapping("/makeTop")
    public ResponseResult makeTop(@RequestBody ScoreDto dto) {
        ApArticle article = new ApArticle();
        article.setFlag((byte) 1);
        article.setId(dto.getArticleId());
        boolean b = apArticleService.updateById(article);
        return b?ResponseResult.okResult(null):ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    @PostMapping("/cancelTop")
    public ResponseResult cancelTop(@RequestBody ScoreDto dto) {
        ApArticle article = new ApArticle();
        article.setFlag((byte) 0);
        article.setId(dto.getArticleId());
        boolean b = apArticleService.updateById(article);
        return b?ResponseResult.okResult(null):ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    @PostMapping("/deleteArticle")
    public ResponseResult deleteArticle(@RequestBody ScoreDto dto) {
        ApArticle article = new ApArticle();
        article.setFlag((byte) 3);
        article.setId(dto.getArticleId());
        boolean b = apArticleService.updateById(article);
        return b?ResponseResult.okResult(null):ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
    @PostMapping("/cancelDelete")
    public ResponseResult cancelDeleteArticle(@RequestBody ScoreDto dto) {
        ApArticle article = new ApArticle();
        article.setFlag((byte) 0);
        article.setId(dto.getArticleId());
        boolean b = apArticleService.updateById(article);
        return b?ResponseResult.okResult(null):ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
    }
}